//
//  ZYCProfileModifyUserInfomationViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/20.
//

#import "ZYCProfileModifyUserInfomationViewController.h"

#import "BTEnterErrorDispose.h"
#import "NSString+File.h"
@interface ZYCProfileModifyUserInfomationViewController ()


@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) ZYCProfileModifyUserInfoView *modifyInfoView;
@property (nonatomic, copy) NSString *roleType;
@end

@implementation ZYCProfileModifyUserInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSInteger roleType =  [[NSUserDefaults standardUserDefaults] integerForKey:@"kUserRoleType"];
    if (roleType  == 0) {//教师
        self.roleType = @"1";
    }else{
        self.roleType = @"2";
    }
    self.title = self.titleString;
    [self.view addSubview:self.modifyInfoView];
    self.modifyInfoView.isShowTip = self.isShowTip;
    self.modifyInfoView.type = self.type;
    self.modifyInfoView.modifyContent = self.modifyContent;
    self.modifyInfoView.placeHolder = self.placeHolder;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveButton];
}


// 修改姓名
- (void)modifyUserName{
    
    NSDictionary *params = @{
        @"realName":self.modifyInfoView.text,
        @"roleType":self.roleType
    };
    [MrjSVProgressHUD show];
    [[URLManager sharedURLManager] postUrlString:Profile_Post_ModifyUserInfo requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
    
}

// 修改手机号
- (void)modifyPhoneNumber{
    
    BTPhoneEmpty(self.modifyInfoView.text);
    BTPhoneValidVerify(self.modifyInfoView.text);    
    
    NSDictionary *params = @{
        @"phonenumber":self.modifyInfoView.text,
        @"roleType":self.roleType
    };
    
    [MrjSVProgressHUD show];
    [[URLManager sharedURLManager] postUrlString:Profile_Post_ModifyUserInfo requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
    
}
- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [[UIButton alloc] init];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:HEX_RGB(kBgWhiteColor) forState:UIControlStateNormal];
        _saveButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        [_saveButton addTarget:self action:@selector(clickSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

- (ZYCProfileModifyUserInfoView *)modifyInfoView {
    if (!_modifyInfoView) {
        _modifyInfoView = [[ZYCProfileModifyUserInfoView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
    }
    return _modifyInfoView;
}

- (void)clickSaveAction:(UIButton *)sender{
    if ([self.titleString isEqualToString:@"修改姓名"]) {
        [self modifyUserName];
    }else{
        [self modifyPhoneNumber];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
